// 生成图片验证码图片
function generateUUID() {
    var d = new Date().getTime();
    if (window.performance && typeof window.performance.now === "function") {
        d += performance.now(); //use high-precision timer if available
    }
    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = (d + Math.random() * 16) % 16 | 0;
        d = Math.floor(d / 16);
        return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
    });
    return uuid;
}

function generateImageCode() {

    // 生成一个编号
    imageCodeId = generateUUID()

    var imageCodeUrl = "/image_code?code_id=" + imageCodeId;

    $(".get_pic_code").attr("src", imageCodeUrl)


}


// 点击登录按钮
$(".login_form .main-btn").click(function () {


    var phone = $("#phone").val();
    var password = $("#password").val();
    var mi_code = $("#mi_code").val();

    // 组织参数
    params = {
        phone: phone,
        password: password,
        mi_code: mi_code,
        active: "do"
    }

    $.ajax({
        url: '/login.html',
        type: "post",
        data: JSON.stringify(params),
        contentType: "application/json",
        success: function (resp) {
            if (resp.errno == "0") {

                alert(resp.errmsg)

                location.href = '/'
            } else {
                alert(resp.errmsg)

                $("#image_code").attr("src", "/image_code?code_id=" + generateUUID())
            }

        },
        error: function () {

            alert("网络错误")

        }
    })
})


//点击注册
$(".rlogin_form .main-btn").click(function () {


    var name = $("#rname").val();
    var phone = $("#rphone").val();
    var password = $("#rpassword").val();
    var re_password = $("#re_password").val()
    var mi_code = $("#rmi_code").val();

    console.log(re_password)
    console.log(password)


    if (!password) {
        alert('密码不能为空')
        return 0;
    }

    if (re_password != password) {

        alert('两次输入的密码不一样')

        return 0;
    }
    if (!mi_code) {

        alert('没有输入验证码')

        return 0;
    }


    // 组织参数
    params = {
        name: name,
        phone: phone,
        password: password,
        mi_code: mi_code,
        active: "undo"
    }

    $.ajax({
        url: '/register.html',
        type: "post",
        data: JSON.stringify(params),
        contentType: "application/json",
        success: function (resp) {

            if (resp.errno == 0) {

                alert(resp.errmsg)

                location.href = '/'
            } else if (resp.errno == '100') {

                alert(resp.errmsg)

                location.href = '/login.html'
            } else {
                alert(resp.errmsg)
            }

        },
        error: function () {
            alert("网络错误")

        }
    })
})

// 获取验证码倒计时

var countdown = 60;

$('#numbtn').on('click', function () {
    var obj = $("#numbtn");

    settime(obj);

    var phone = $("#rphone").val()

    if (!phone) {
        alert("请输入手机号")
    }

    $.ajax({
        url: "/get_note_code",
        type: "GET",
        data: {phone: phone},
    }).success(function (resp) {

        alert(resp.errmsg)

    })

})

function settime(obj) { //发送验证码倒计时

    if (countdown == 0) {
        obj.attr('disabled', false);

        //obj.removeattr("disabled");

        obj.html("免费获取验证码");

        countdown = 60;

        return;

    } else {
        obj.attr('disabled', true);

        obj.html("重新发送" + countdown + "s");

        countdown--;

    }

    setTimeout(function () {
            settime(obj)

        }

        , 1000)

}

//点击 找回密码
$(".slogin_form .main-btn").click(function () {


    var phone = $("#rphone").val();
    var password = $("#rpassword").val();
    var re_password = $("#re_password").val();
    var mi_code = $("#rmi_code").val();

    if (re_password != password) {

        alert("两次输入的密码不一致");
        return 0;
    }
    // 组织参数
    params = {
        phone: phone,
        password: password,
        mi_code: mi_code,
        active: "ando"
    }

    $.ajax({
        url: '/reset.html',
        type: "post",
        data: JSON.stringify(params),
        contentType: "application/json",
        success: function (resp) {

            if (resp.errno == 0) {

                alert(resp.errmsg)

                location.href = '/login.html'
            } else {
                alert(resp.errmsg)
            }

        },
        error: function () {
            alert("网络错误")

        }
    })
})



